package pure.csw.activemq.persistence.topic;

import pure.csw.common.ActiveMqUtils;
import pure.csw.common.Constant;

import javax.jms.*;

public class Subscriber {
    public static void main(String[] args) throws JMSException {
        Connection connection = ActiveMqUtils.getConnection();
        connection.setClientID("alichoto");
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(Constant.TOPICNAME);
        //创建持久化订阅者
        TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subscriber");
        //connection.start();
        System.out.println("接受消息。。。");
        //接受消息
        while(true){
            TextMessage m = (TextMessage) subscriber.receive(1000);
            if (m != null){
                System.out.println(m.getText());
            }else{
                break;
            }
        }
        subscriber.close();
        session.close();
        connection.close();
        System.out.println("程序结束");
    }
}
